<?php
class Model_LibraryAcl extends Zend_Acl {
	
	protected $_roles;
	protected $_modules;
	protected $_controllers;
	protected $_actions;
	protected $_db;
	protected $_user;
	public $_getUserRoleName = null;
	public $_getUserRoleId = null;
	
        public function __construct() {
    	$this->_db = Zend_Db::factory('Pdo_Pgsql', array(
    		'host'       => 'localhost',
    		'username'   => 'postgres',
    		'password'   => 'sl2003br',
    		'dbname'     => 'genya',
    		//'persistent' => true
		));
		//$user = Zend_Registry::get('role');
//		$this->_user = $user ? $user : 'Guest';
//		$roles = $this->listRoles();
//		$this->_getUserRoleId = $getUserRole['role_id'] ? $getUserRole['role_id'] : 4;
//		$this->_getUserRoleName = $getUserRole['role_name'] ? $getUserRole['role_name'] : ‘User’;
//		$this->addRole(new Zend_Acl_Role($this->_user), $this->_getUserRoleName);

		//$this->initRoles();
		
        $this->addRole(new Zend_Acl_Role('guests'));
        $this->addRole(new Zend_Acl_Role('users'), 'guests');
        $this->addRole(new Zend_Acl_Role('admins'), 'users');
		
		$this->add(new Zend_Acl_Resource('library'))
			 ->add(new Zend_Acl_Resource('library:books'), 'library');
			 
		$this->add(new Zend_Acl_Resource('admin'))
			 ->add(new Zend_Acl_Resource('admin:book'), 'admin')
			 ->add(new Zend_Acl_Resource('admin:module'), 'admin')
			 ->add(new Zend_Acl_Resource('admin:role'), 'admin')
			 ->add(new Zend_Acl_Resource('admin:categoria'), 'admin')
             ->add(new Zend_Acl_Resource('admin:controllers'), 'admin')
             ->add(new Zend_Acl_Resource('admin:controledocumentos'), 'admin')
             ->add(new Zend_Acl_Resource('admin:action'), 'admin')
             ->add(new Zend_Acl_Resource('admin:menu'), 'admin')
             ->add(new Zend_Acl_Resource('admin:submenu'), 'admin')
             ->add(new Zend_Acl_Resource('admin:rolesubmenu'), 'admin')
                        ->add(new Zend_Acl_Resource('admin:user'), 'admin');
			 
		$this->add(new Zend_Acl_Resource('default'))
			 ->add(new Zend_Acl_Resource('default:authentication'), 'default')
			 ->add(new Zend_Acl_Resource('default:index'), 'default')
			 ->add(new Zend_Acl_Resource('default:error'), 'default');
			 
		$this->allow('guests', 'default:authentication', 'login');
		$this->allow('guests', 'default:error', 'error');
		
		$this->deny('users', 'default:authentication', 'login');
		$this->allow('users', 'default:index', 'index');
		$this->allow('users', 'default:authentication', 'logout');
		$this->allow('users', 'library:books', array('index', 'list'));
		$this->allow('users', 'admin:controledocumentos', array('index'));
		
		$this->allow('admins', 'admin:book', array('index', 'add', 'edit','delete'));
		$this->allow('admins', 'admin:module', array('index','add','edit','delete'));
        $this->allow('admins', 'admin:controllers', array('index','add','edit','delete'));
		$this->allow('admins', 'admin:role', array('index','add','edit','delete'));
		$this->allow('admins', 'admin:categoria', array('index','add','edit','delete'));
		$this->allow('admins', 'admin:controledocumentos', array('index','dados','save','dadosall','savedoc'));
        $this->allow('admins', 'admin:action', array('index','add','edit','delete'));                
        $this->allow('admins', 'admin:menu', array('index','add','edit','delete'));
        $this->allow('admins', 'admin:submenu', array('index','add','edit','delete','montacombocontroller','montacomboactions'));
        $this->allow('admins', 'admin:rolesubmenu', array('index','add','edit','delete'));
        $this->allow('admins', 'admin:user', array('index','add','edit','delete'));
    }
    
    private function initRoles(){
    	$selectRoles = $this->_db->select()
							->from('acl_role');
		
		$roles = $this->_db->fetchAll($selectRoles);
		
		$this->addRole(new Zend_Acl_Role('guests'));
    	for ($i = 0; $i < count($roles); $i++) {
			$this->addRole(new Zend_Acl_Role($roles[$i]['role_name']), $roles[$i-1]['role_name']);
		}
		
    }
    
    private function initResources(){
    	
    }
    
   public function listRoles() {
		$select = $this->_db->select()
								->from(array('a'=>'acl_role'),'a.*')
								->join(array('b' => 'acl_user'),'b.role_id = a.role_id',array())
								->where("b.user_name = '". $this->_user."'");
		echo "<pre> Dados: ";
			print($select);
		echo "</pre>";
		die();
		$roles = $this->_db->fetchAll($select);
		
		return $roles;
    }
}
